﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace FilesTubeClient
{
    public class Request
    {
        internal const string basicRequestFormat = "http://api.filestube.com/?key={0}&phrase={1}";

        //--------------------------------------------------------------------------------
        public enum Sorts
        {
            none,
            size_ascending,
            size_descending,
            date_ascending,
            date_descending
        }
        //--------------------------------------------------------------------------------
        public enum Extensions
        {
            none,
            rar,
            wmv,
            avi,
            mpg,
            mpeg,
            mp3,
            wma,
            zip
        }
        //--------------------------------------------------------------------------------
        public string Key
        {
            get { return global::FilesTubeClient.Properties.Settings.Default.EncryptedFilestubeKey; }
        }
        //--------------------------------------------------------------------------------
        private string _phrase = "";
        public string Phrase
        {
            get { return _phrase; }
            set { _phrase = value; }
        }
        //--------------------------------------------------------------------------------
        private string _related = "";
        public string Related
        {
            get { return _related; }
            set { _related = value; }
        }
        //--------------------------------------------------------------------------------
        private Sorts _sort = Sorts.none;
        public Sorts Sort
        {
            set { _sort = value; }
        }
        public string SortString
        {
            get
            {
                switch(_sort)
                {
                    case Sorts.none: return "";
                    case Sorts.date_ascending: return "da";
                    case Sorts.date_descending: return "dd";
                    case Sorts.size_ascending: return "sa";
                    case Sorts.size_descending: return "sd";
                    default: return "";
                }
            }
        }
        //--------------------------------------------------------------------------------
        private uint _page = 1;
        public uint Page
        {
            get { return _page; }
            set { _page = value; }
        }
        //--------------------------------------------------------------------------------
        private Extensions _extension = Extensions.none;
        public Extensions Extension
        {
            get { return _extension; }
            set { _extension = value; }
        }
        //--------------------------------------------------------------------------------
        public Request(string phrase)
        {
            _phrase = phrase;
        }
        //--------------------------------------------------------------------------------
        public override string ToString()
        {
            string request = string.Format(basicRequestFormat, Key, Phrase);

            if (Page > 1)
                request += "&Page=" + Page;

            if(Related != "")
                request += "&related=" + Related;

            if (SortString != "")
                request += "&sort=" + SortString;

            if(Page > 1)
                request += "&page=" + Page;

            if(Extension != Extensions.none)
                request += "&extension=" + Extension.ToString();

            return request;
        }
        //--------------------------------------------------------------------------------
        public Page performRequest()
        {
            WebClient wc = new WebClient();
            string responseXml = wc.DownloadString(this.ToString());
            return Response.parse(responseXml);
        }
        //--------------------------------------------------------------------------------
    }
}
